import java.awt.*;
import java.io.*;

class CLine extends CObject
{
	Anchor[] a;						// start & end points

	float kf,b;

	CLine(int type,int subtype,int x1,int y1,int x2,int y2)
	{
		super(type,subtype);
		a = new Anchor[2];
		a[0] = new Anchor(x1,y1);
		a[1] = new Anchor(x2,y2);
		init();
	}

	void init()
	{
		xmin = (a[0].x < a[1].x) ? a[0].x : a[1].x;
		xmax = (a[0].x > a[1].x) ? a[0].x : a[1].x;
		ymin = (a[0].y < a[1].y) ? a[0].y : a[1].y;
		ymax = (a[0].y > a[1].y) ? a[0].y : a[1].y;

		kf = ((float)a[1].y - (float)a[0].y) / ((float)a[1].x - (float)a[0].x);
		b = (float)a[0].y - kf * (float)a[0].x;

		x1 = a[0].x;
		y1 = a[0].y;
		x2 = a[1].x;
		y2 = a[1].y;
		if (x1 > x2)
	    {
	    	dx = x1;
	    	x1 = x2;
	    	x2 = dx;
	    	dy = y1;
	    	y1 = y2;
	    	y2 = dy;
	    }

		dy = y2 - y1;
		dx = x2 - x1;

		if (dx >= 10)
		{
			imageIndex = 6 * subtype;
			k = dy * 10 / dx;
			if (Math.abs(dy * 100 / dx) % 10 >= 5) k += (dx * dy >= 0) ? 1 : -1;
			if (k == 2) imageIndex += 1;
			if (k == -2) imageIndex += 2;
			disp = (k < 0) ? k + 1 : 0;
		}
	}

	boolean checkActive(int mx,int my)
	{
		active = false;
	    if ((mx >= xmin) && (mx <= xmax))
	    {
	    	a[0].active = (a[0].x - 6 <= mx) && (a[0].x + 6 >= mx);
	    	a[1].active = (a[1].x - 6 <= mx) && (a[1].x + 6 >= mx);
			int y = (int)(mx * kf + b);
			active = ((my >= y) && (y + 8 >= my));
	    }
	    return active;
	}

	void draw(Graphics g)
	{
		boolean mode = (type == MainFrame.activeType);
		g.setColor(selected ? Color.red : (mode ? color : color2));
		if (blink || !active || selected)
		{
			g.drawLine(a[0].x,a[0].y,a[1].x,a[1].y);
			a[0].draw(g,mode);
			a[1].draw(g,mode);
		}
		blink = !blink;
	}

	void move(int mx,int my,int start_x,int start_y)
	{
		if (a[0].active) moveAnchor(0,mx,my);
		else
		if (a[1].active) moveAnchor(1,mx,my);
		else
		{
			a[0].move(mx - start_x,my - start_y);
			a[1].move(mx - start_x,my - start_y);
		}
		init();
	}

	void moveAnchor(int index,int mx,int my)
	{
		int dx = mx - a[1 - index].x;
		int dy = my - a[1 - index].y;
		if (Math.abs(dx) >= 10)
		{
			dx = (int)(dx / 10) * 10;
			kf = ((float)((int)(dy * 10 / dx))) / 10;
			if (kf > 0.1f) kf = 0.2f;
			else if (kf > -0.1) kf = 0f;
			else kf = - 0.2f;
			dy = (int)(dx * kf);
   			a[index].x = a[1 - index].x + dx;
   			a[index].y = a[1 - index].y + dy;
   			init();
		}
	}
	void write(DataOutputStream out) throws IOException
	{
		out.writeByte((byte)type);
		out.writeByte((byte)subtype);
		out.writeShort((short)a[0].x);
		out.writeShort((short)a[0].y);
		out.writeShort((short)a[1].x);
		out.writeShort((short)a[1].y);
	}

	static CObject read(DataInputStream in,int type) throws IOException
	{
		int subtype = in.readByte();
		int x1 = in.readShort();
		int y1 = in.readShort();
		int x2 = in.readShort();
		int y2 = in.readShort();
		return new CLine(type,subtype,x1,y1,x2,y2);
	}
}                
